package com.hydl.power.service.impl;

import com.hydl.power.mapper.CityMapper;
import com.hydl.power.mapper.FaultAnalyzeMapper;
import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.pojo.City;
import com.hydl.power.pojo.FaultAnalyze;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.CityService;
import com.hydl.power.service.FaultAnalyzeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;

@Service
public class FaultAnalyzeServiceImpl implements FaultAnalyzeService {

    @Autowired
    private FaultAnalyzeMapper faultAnalyzeMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public FaultAnalyze queryFaultAnalyze(String cityId, String projectId, String roomId,String token,String startTime,String endTime) {
        DecimalFormat df = new DecimalFormat("0.00%");
        SysUser sysUser =sysUserMapper.getUserByToken(token);
        if(sysUser!=null){
            if(sysUser.getStatus()==2) {
                FaultAnalyze faultAnalyze = faultAnalyzeMapper.queryFaultAnalyze(cityId,sysUser.getProjectId().toString(),roomId,startTime,endTime);
                if(faultAnalyze.getSumCount().equals("0")){
                    faultAnalyze.setOutOfLimitPercent("0%");
                    faultAnalyze.setDeflectionPercent("0%");
                    faultAnalyze.setPowerReversalPercent("0%");
                    faultAnalyze.setTimeReversalPercent("0%");
                    faultAnalyze.setDataAnomalyPercent("0%");
                    faultAnalyze.setCommunicationPercent("0%");
                    faultAnalyze.setGeneralCountPercent("0%");
                    faultAnalyze.setExigencyCountPercent("0%");
                    faultAnalyze.setSeriousnessCountPercent("0%");
                }else {
                    faultAnalyze.setOutOfLimitPercent(df.format(Double.parseDouble(faultAnalyze.getOutOfLimit())/Double.parseDouble(faultAnalyze.getSumCount())));
                    faultAnalyze.setDeflectionPercent(df.format(Double.parseDouble(faultAnalyze.getDeflection())/Double.parseDouble(faultAnalyze.getSumCount())));
                    faultAnalyze.setPowerReversalPercent(df.format(Double.parseDouble(faultAnalyze.getPowerReversal())/Double.parseDouble(faultAnalyze.getSumCount())));
                    faultAnalyze.setTimeReversalPercent(df.format(Double.parseDouble(faultAnalyze.getTimeReversal())/Double.parseDouble(faultAnalyze.getSumCount())));
                    faultAnalyze.setDataAnomalyPercent(df.format(Double.parseDouble(faultAnalyze.getDataAnomaly())/Double.parseDouble(faultAnalyze.getSumCount())));
                    faultAnalyze.setCommunicationPercent(df.format(Double.parseDouble(faultAnalyze.getCommunication())/Double.parseDouble(faultAnalyze.getSumCount())));
                    faultAnalyze.setGeneralCountPercent(df.format(Double.parseDouble(faultAnalyze.getGeneralCount())/Double.parseDouble(faultAnalyze.getSumCount())));
                    faultAnalyze.setExigencyCountPercent(df.format(Double.parseDouble(faultAnalyze.getExigencyCount())/Double.parseDouble(faultAnalyze.getSumCount())));
                    faultAnalyze.setSeriousnessCountPercent(df.format(Double.parseDouble(faultAnalyze.getSeriousnessCount())/Double.parseDouble(faultAnalyze.getSumCount())));
                }
                return faultAnalyze;
                 }
            }
        FaultAnalyze faultAnalyze = faultAnalyzeMapper.queryFaultAnalyze(cityId,projectId,roomId,startTime,endTime);
        if(faultAnalyze.getSumCount().equals("0")){
            faultAnalyze.setOutOfLimitPercent("0%");
            faultAnalyze.setDeflectionPercent("0%");
            faultAnalyze.setPowerReversalPercent("0%");
            faultAnalyze.setTimeReversalPercent("0%");
            faultAnalyze.setDataAnomalyPercent("0%");
            faultAnalyze.setCommunicationPercent("0%");
            faultAnalyze.setGeneralCountPercent("0%");
            faultAnalyze.setExigencyCountPercent("0%");
            faultAnalyze.setSeriousnessCountPercent("0%");
        }else {
            faultAnalyze.setOutOfLimitPercent(df.format(Double.parseDouble(faultAnalyze.getOutOfLimit())/Double.parseDouble(faultAnalyze.getSumCount())));
            faultAnalyze.setDeflectionPercent(df.format(Double.parseDouble(faultAnalyze.getDeflection())/Double.parseDouble(faultAnalyze.getSumCount())));
            faultAnalyze.setPowerReversalPercent(df.format(Double.parseDouble(faultAnalyze.getPowerReversal())/Double.parseDouble(faultAnalyze.getSumCount())));
            faultAnalyze.setTimeReversalPercent(df.format(Double.parseDouble(faultAnalyze.getTimeReversal())/Double.parseDouble(faultAnalyze.getSumCount())));
            faultAnalyze.setDataAnomalyPercent(df.format(Double.parseDouble(faultAnalyze.getDataAnomaly())/Double.parseDouble(faultAnalyze.getSumCount())));
            faultAnalyze.setCommunicationPercent(df.format(Double.parseDouble(faultAnalyze.getCommunication())/Double.parseDouble(faultAnalyze.getSumCount())));
            faultAnalyze.setGeneralCountPercent(df.format(Double.parseDouble(faultAnalyze.getGeneralCount())/Double.parseDouble(faultAnalyze.getSumCount())));
            faultAnalyze.setExigencyCountPercent(df.format(Double.parseDouble(faultAnalyze.getExigencyCount())/Double.parseDouble(faultAnalyze.getSumCount())));
            faultAnalyze.setSeriousnessCountPercent(df.format(Double.parseDouble(faultAnalyze.getSeriousnessCount())/Double.parseDouble(faultAnalyze.getSumCount())));
        }

        return faultAnalyze;
    }
}
